﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Config;
using AMM.Model;
using AMM.Model.Config;

namespace AMM.Business.Config {
    public class UserProcessor : BaseProcessor {

        private UserRepository _userRepository;

        public UserProcessor() {
            this._userRepository = new UserRepository();
        }

        public User UserLogin(string userName, string password) {
            try {
                return this._userRepository.FetchUserList().Find(x => x.Name == userName && x.Password == password);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("用户名或密码错误！");
            }
        }

        public List<User> FetchUserList(int roleId = 0) {
            try {
                if (roleId == 0) {
                    return this._userRepository.FetchUserList();
                }
                else {
                    return this._userRepository.FetchUserByRole(roleId);
                }
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取用户列表失败！");
            }
        }

        public List<UserViewModel> FetchUserViewModelList(int roleId = 0) {
            try {
                List<User> resultList;
                if (roleId == 0) {
                    resultList = this._userRepository.FetchUserList();
                }
                else {
                    resultList = this._userRepository.FetchUserByRole(roleId);
                }

                return resultList.Select(x => {
                    return new UserViewModel {
                        Key = x.UserId,
                        Name = x.Name,
                        Password = x.Password,
                        Description = x.Description,
                        IsEnabled = x.IsEnabled,
                        IsSystem = x.IsSystem,
                        CreatedBy = x.CreatedBy,
                        ModifiedBy = x.ModifiedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取用户列表失败！");
            }
        }

        public User FetchUserById(int userId) {
            try {
                User user = this._userRepository.FetchUserByUserId(userId);
                if (user == null) {
                    user = new User();
                }
                return user;
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定用户失败！");
            }
        }

        public Dictionary<string, int> FetchUserAllMenuPermission(int userId) {
            try {
                return this._userRepository.FetchUserAllMenuPermission(userId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定用户菜单权限失败！");
            }
        }

        public void Insert(User user) {
            try {
                user.UserId = Guid.NewGuid().GetHashCode();
                user.UserRoles.ToList().ForEach(x => {
                    x.UserId = user.UserId;
                    x.URId = Guid.NewGuid().GetHashCode();
                });
                user.CreatedDatetime = DateTime.Now;
                this._userRepository.Insert(user);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("插入新用户失败！");
            }
        }

        public void Update(User user) {
            try {
                User oldUser = this.FetchUserById(user.UserId);
                if (!oldUser.RowVersion.SequenceEqual(user.RowVersion)) {
                    throw this.BuildException("更新用户失败，数据不一致性错误！");
                }

                user.UserRoles.ToList().ForEach(x => {
                    x.URId = Guid.NewGuid().GetHashCode();
                });
                user.ModifiedDatetime = DateTime.Now;
                this._userRepository.Update(user);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新用户失败！");
            }
        }

        public void Delete(User user) {
            try {
                this._userRepository.Delete(user);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除用户失败！");
            }
        }

        public void Delete(params int[] userIds) {
            try {
                this._userRepository.DeleteMultipleUser(userIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除用户失败！");
            }
        }
    }
}
